
module Flgrdisplay
  require 'flgrcore'
  include Flgrcore

  def tmpfilename(size=16)
    s = ""
    size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
    s
  end
  
 
  def flgr_display(arg1, *rest)
    tab_im=[]
    tab_fi=[]

    if arg1.class == Flgrcore::FLGR_Data2D
      tab_im << arg1
    elsif arg1.class == String
      tab_fi << arg1
    end
    
    ## sort image and string in two different array
    rest.each do |arg|
      if arg.class == Flgrcore::FLGR_Data2D
        tab_im << arg
      elsif arg.class == String
        tab_fi << arg+'.png'
      end
    end

    ## create some random string if there is not enough strings given
    if tab_fi.length < tab_im.length
      for i in 0..(tab_im.length-tab_fi.length-1)
        tab_fi << tmpfilename(8)+'.png'
      end
    end

    ## construct command line argument
    files = ""
    for i in 0..(tab_im.length-1)
      flgr2d_save_png(tab_im[i],tab_fi[i])
      files = files + tab_fi[i] + " "
    end

    ## launch nxv
    if RUBY_PLATFORM["linux"]=="linux"
      fork {system("nxv -RM #{files}")}

    elsif RUBY_PLATFORM["win"]=="win"
      require 'win32/process'
      nxv_path = ""

      #get nxv installation path in the windows registry
      Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\NxV') do |reg|
        k, nxv_path=reg.read('Install_Dir')
      end 
      
      nxv_path = nxv_path + '-RM ' + files
      fork {system(nxv_path)}
    end


  end



end
  


